package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;

import utils.DBG;
import core.ImageLibrary;

public class GameButton extends MenuItem {
	String text;

	Image clicked_image;
	Image over_image;
	Image static_image;
	double button_scale;

	Vector<GameButtonListener> listeners;

	int function_code = -1;

	public GameButton() {

	}

	public GameButton(String text_, int width, int height) {
		super(0, 0);
		text = text_;
		this.setSize(new Dimension(width, height));
		generateImage();
		listeners = new Vector<GameButtonListener>();
		button_scale = 1;

	}

	@Override
	public void generateImage() {
		// TODO Auto-generated method stub
		static_image = this.getButtonImage("button_r_blue");
		over_image = this.getButtonImage("button_r_blue_over");
		clicked_image = this.getButtonImage("button_r_blue_clicked");
		image = static_image;
	}

	public void addGameButtonListener(GameButtonListener l) {
		listeners.add(l);
	}

	@Override
	public void renderComponent(Graphics2D g2d) {
		// TODO Auto-generated method stub
		if (image != null) {
			double scal = 1.0;
			if (image == over_image) {
				scal = 1.1;
			} else {
				scal = button_scale;
			}
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.scale(scal, scal);
			int ancho = ((int) (image.getWidth(null) * scal) - image
					.getWidth(null)) / 2;
			int alto = ((int) (image.getHeight(null) * scal) - image
					.getHeight(null)) / 2;
			g2d.drawImage(image, (int) ((getX() - ancho) / scal),
					(int) ((getY() - alto) / scal), null);
			g2d.scale(1 / scal, 1 / scal);
		}

	}

	public Image getButtonImage(String name) {
		Image img = null;

		Image up_left_corner = ImageLibrary.getInstance()
				.getImage(name + "_01");
		Image up_right_corner = ImageLibrary.getInstance().getImage(
				name + "_03");
		Image bottom_left_corner = ImageLibrary.getInstance().getImage(
				name + "_07");
		Image bottom_right_corner = ImageLibrary.getInstance().getImage(
				name + "_09");

		int ancho_scaled = this.getSize().width
				- (up_left_corner.getWidth(null) + up_right_corner
						.getWidth(null));
		int alto_scaled = this.getSize().height
				- (up_left_corner.getHeight(null) + bottom_left_corner

				.getHeight(null));

		DBG.showMsg(ancho_scaled + "   " + alto_scaled, this);

		Image up_middle = ImageLibrary.getInstance().getScaledImage(
				name + "_02", ancho_scaled, up_left_corner.getHeight(null));

		Image left_middle = ImageLibrary.getInstance().getScaledImage(
				name + "_04", up_left_corner.getWidth(null), alto_scaled);
		Image right_middle = ImageLibrary.getInstance().getScaledImage(
				name + "_06", up_right_corner.getWidth(null), alto_scaled);
		Image bottom_middle = ImageLibrary.getInstance().getScaledImage(
				name + "_08", ancho_scaled, bottom_left_corner.getHeight(null));

		Image middle = ImageLibrary.getInstance().getScaledImage(name + "_05",
				ancho_scaled, alto_scaled);

		img = ImageLibrary.getInstance().createEmptyImage(this.getSize().width,
				this.getSize().height);
		Graphics gimg = img.getGraphics();

		gimg.drawImage(up_left_corner, 0, 0, null);
		gimg.drawImage(up_middle, up_left_corner.getWidth(null), 0, null);
		gimg.drawImage(up_right_corner, up_left_corner.getWidth(null)
				+ up_middle.getWidth(null), 0, null);

		gimg.drawImage(left_middle, 0, up_left_corner.getHeight(null), null);
		gimg.drawImage(middle, left_middle.getWidth(null), up_left_corner
				.getHeight(null), null);
		gimg.drawImage(right_middle, left_middle.getWidth(null)
				+ middle.getWidth(null), up_left_corner.getHeight(null), null);

		gimg.drawImage(bottom_left_corner, 0, up_left_corner.getHeight(null)
				+ left_middle.getHeight(null), null);
		gimg.drawImage(bottom_middle, bottom_left_corner.getWidth(null),
				up_left_corner.getHeight(null) + left_middle.getHeight(null),
				null);
		gimg.drawImage(bottom_right_corner, bottom_left_corner.getWidth(null)
				+ bottom_middle.getWidth(null), up_left_corner.getHeight(null)
				+ left_middle.getHeight(null), null);

		gimg.drawString(text, 10, 20);

		return img;
	}

	@Override
	public void updateMouse(Point p) {
		// TODO Auto-generated method stub

		if (this.inside(p)) {
			if (image != over_image) {
				image = over_image;
			}
			setAlpha(1.0f);
		} else {
			if (image != static_image) {
				image = static_image;
			}
			setAlpha(0.5f);
		}
	}

	@Override
	public boolean onClick(MouseEvent e) {
		// TODO Auto-generated method stub
		if (this.inside(e.getPoint())) {
			if (image != clicked_image) {
				image = clicked_image;
				Iterator<GameButtonListener> it = listeners.iterator();
				while (it.hasNext()) {
					GameButtonListener blist = it.next();
					if (blist.doAction(this.function_code)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	private boolean inside(Point p) {
		return (p.x < this.getX() + this.getWidth() && p.x > this.getX())
				&& (p.y < this.getY() + this.getHeight() && p.y > this.getY());
	}

	public void addFunction(int function_code) {
		this.function_code = function_code;
	}

}
